package com.example.taobaounion3.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.taobaounion3.R;
import com.example.taobaounion3.base.BaseActivity;
import com.example.taobaounion3.model.domain.TicketResult;
import com.example.taobaounion3.presenter.ITicketPresenter;
import com.example.taobaounion3.utils.LogUtils;
import com.example.taobaounion3.utils.PresenterManager;
import com.example.taobaounion3.utils.ToastUtil;
import com.example.taobaounion3.view.ITicketPagerCallback;

import butterknife.BindView;

public class TicketActivity extends BaseActivity implements ITicketPagerCallback {

    private ITicketPresenter mTicketPresenter;


    private boolean mHasTabaoApp =false;

    @BindView(R.id.ticket_back_press)
    public View backPress;

    @BindView(R.id.ticket_cover)
    public ImageView mCover;

    @BindView(R.id.ticket_code)
    public EditText mTicketCode;

    @BindView(R.id.ticket_copy_or_open_btn)
    public TextView mOpenOrCapyBtn;

    @BindView(R.id.ticket_cover_loading)
    public View loadingView;

    @BindView(R.id.ticket_load_retry)
    public TextView retryLoadText;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_ticket;
    }



    @Override
    protected void initPresenter() {
        mTicketPresenter= PresenterManager.getInstance().getmTicketPresenter();
        if(mTicketPresenter!=null) {
            mTicketPresenter.registerViewCallback(this);
        }
        //判断是否有安装淘宝
        //com.taobao.taobao,检查是否安装淘宝应用
        PackageManager pm=getPackageManager();
        try {
            PackageInfo packageInfo = pm.getPackageInfo("com.taobao.taobao", PackageManager.MATCH_UNINSTALLED_PACKAGES);
            mHasTabaoApp =packageInfo!=null;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            mHasTabaoApp =false;
        }
        LogUtils.d(this,"mMasTabaoApp-->"+ mHasTabaoApp);
        //根据mMasTabaoApp的值去修改UI
        mOpenOrCapyBtn.setText(mHasTabaoApp?"打开淘宝领券":"复制淘口令");
    }



    @Override
    protected void release() {
        if (mTicketPresenter!=null) {
            mTicketPresenter.unregisterViewCallback(this);
        }
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initEvent() {
        backPress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mOpenOrCapyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //复制淘口令
                //拿到内容
                String ticketCode = mTicketCode.getText().toString().trim();
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("sob_taobao_ticket_code", ticketCode);
                cm.setPrimaryClip(clipData);
                //判断有没有淘宝
                if (mHasTabaoApp) {
                    //如果有就打开淘宝
                    Intent taobaoIntent = new Intent();
                    ComponentName componentName=new ComponentName("com.taobao.taobao","com.taobao.tao.TBMainActivity");
                    taobaoIntent.setComponent(componentName);
                    startActivity(taobaoIntent);
                }else {
                    //如果没有就提示复制成功
                    ToastUtil.showToast("已经复制，粘贴分享或打开淘宝");
                }


            }
        });
    }

    @Override
    public void onTicketLoaded(String cover, TicketResult result) {
        if (mCover!=null&& !TextUtils.isEmpty(cover)) {
            //String coverPath=urlUtils.getCoverPath(cover);
            //TODO:
            LogUtils.d(this,"coverPath-->"+cover);
            Glide.with(this).load(cover).into(mCover);
        }
        if (result!=null&&result.getData().getTbk_tpwd_create_response()!=null){
            mTicketCode.setText(result.getData().getTbk_tpwd_create_response().getData().getModel());
        }
        if (loadingView!=null) {
            loadingView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onError() {
        if (loadingView!=null) {
            loadingView.setVisibility(View.GONE);
        }
        if (retryLoadText!=null) {
            retryLoadText.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onLoading() {
        if (retryLoadText!=null) {
            retryLoadText.setVisibility(View.GONE);
        }
        if (loadingView!=null) {
            loadingView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onEmpty() {

    }
}
